package com.cea.papyrus.core.treeeditpart;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.graphics.Image;
import org.eclipse.uml2.uml.BehavioredClassifier;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Namespace;
import org.eclipse.uml2.uml.State;
import org.eclipse.uml2.uml.Transition;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.treeeditpart.pseudoelement.TriggersGroup;

/**
 * Tree edit part for trigger groups
 */
public class GroupingTriggersTreeEditPart extends GroupingTreeEditPart {

	/** associated pseudo element */
	protected TriggersGroup triggersGroup;
	
	/**
	 * @param element
	 * @param modelManager
	 */
	public GroupingTriggersTreeEditPart(TriggersGroup triggersGroup, ModelManager modelManager) {
		super(triggersGroup, modelManager);
		this.triggersGroup = triggersGroup;
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.treeeditpart.CommonTreeEditPart#getText()
	 */
	@Override
	public String getText() {
		Namespace nameSpace = triggersGroup.getNamespace();
		String text = "";
		if(nameSpace instanceof Transition)
			text = "Triggers ("+((Transition)nameSpace).getTriggers().size()+")";
		else if(nameSpace instanceof State)
			text = "Triggers ("+((State)nameSpace).getDeferrableTriggers().size()+")";
		else if(nameSpace instanceof BehavioredClassifier)
			text = "Triggers ("+((BehavioredClassifier)nameSpace).getOwnedTriggers().size()+")";
		return text;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.treeeditpart.CommonTreeEditPart#getImage()
	 */
	@Override
	public Image getImage() {
		return PapyrusPlugin.getImage(PapyrusPlugin.PAPYRUS_ICONS_16x16+"TriggersGroup.gif");
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#getModelChildren()
	 */
	@Override
	protected List<Element> getModelChildren() {
		List<Element> contained = new ArrayList<Element>();
		Namespace nameSpace = triggersGroup.getNamespace();
		if(nameSpace instanceof Transition)
			contained.addAll(((Transition)nameSpace).getTriggers());
		else if(nameSpace instanceof State)
			contained.addAll(((State)nameSpace).getDeferrableTriggers());
		else if(nameSpace instanceof BehavioredClassifier)
			contained.addAll(((BehavioredClassifier)nameSpace).getOwnedTriggers());
		return contained;
	}
}
